﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTCash.Redis;

namespace Uranus.Service
{
    public class BaseData
    {
        public T GetData<T>(Func<object> action, CacheKey cacheKey)
        {
            if (cacheKey == null)
            {
                return default(T);
            }

            if (CacheRedis.ExistsRedis(cacheKey))
            {
                return (T)CacheRedis.GetCache(cacheKey);
            }
            else
            {
                T obj = (T)action();
                if (obj != null)
                {
                    CacheRedis.AddCache(obj, cacheKey);
                    return obj;
                }
                else
                {
                    return default(T);
                }
            }
        }

        public T GetData<T>(Func<object> action, TimeSpan timeSpan, CacheKey cacheKey)
        {
            if (cacheKey == null)
            {
                return default(T);
            }

            if (CacheRedis.ExistsRedis(cacheKey))
            {
                return (T)CacheRedis.GetCache(cacheKey);
            }
            else
            {
                T obj = (T)action();
                if (obj != null)
                {
                    CacheRedis.AddCache(timeSpan, cacheKey);
                    return obj;
                }
                else
                {
                    return default(T);
                }
            }
        }

    }
}
